$(document).ready(function() {
	selida.setup({loginPage: true});
	Login.kodikosDOM = $('input[name="kodikos"]');
	Login.loginDOM = $('input[name="login"]');
	Login.formaDOM = $('.formaSoma');

	if (Login.loginDOM.val()) Login.kodikosDOM.focus();
	else Login.loginDOM.focus();
});

Login = {};

Login.submit = function(form) {
	var params;

	if (Login.proxori === false) return false;
	if (Login.proxori === true) {
		Login.proxori = false;
		return true;
	}
	Login.proxori = false;

	client.session.pektis = Login.loginDOM.val();
	client.session.klidi = globals.randomString(10);

	Login.working();
	client.fyi.pano('Έλεγχος στοιχείων εισόδου. Παρακαλώ περιμένετε…');
	client.node('sinedriaNea', 'kodikos=' + Login.kodikosDOM.val().uri(), function(rsp) {
		if (Login.apotixia(rsp)) return;

		Login.working('prasino.gif');
		client.fyi.pano('Δημιουργία νέας συνεδρίας. Παρακαλώ περιμένετε…');
		client.ajax('login/checkin', 'klidi=' + client.session.klidi.uri(), function(rsp) {
			if (Login.apotixia(rsp)) return;

			client.fyi.pano();
			Login.proxori = true;
			$(form).submit();
		});
	});

	return false;
}

Login.apotixia = function(rsp) {
	Login.working(false);
	Login.kodikosDOM.select();

	if (rsp.sfalma) {
		client.ixos.error();
		delete Login.proxori;
		return true;
	}

	if (rsp.responseText) {
		client.fyi.spano(rsp.responseText);
		delete Login.proxori;
		return true;
	}

	return false;
}

Login.working = function(prm) {
	$('#workingIcon').remove();
	if (prm === false) return;
	if (prm === undefined) prm = 'default.gif';

	$('<img>').attr({
		id: 'workingIcon',
		src: '../ikona/working/' + prm,
	}).appendTo(Login.formaDOM);
}
